# -*- coding: utf-8 -*-
import sys,os
from pipe import Pipe
import threading
from req import  Req
import Util#Util.getDict
import random
from factoryReq import  ReqFactory
from mongoBase import MongoDBBase
from factoryFetcher import Fetcher
sys.path.append("..")
import Util#Util.getDict
def sf_get_data_user_history_ans(req):
    data_list=req.data_info["dataset_uid_history_ans"]["data_list"]
    return data_list
def sf_get_data_pic_history_ans(req):
    data_list=req.data_info["dataset_pid_history_ans"]["data_list"]
    return data_list
def sf_get_data_tag_history_ans(req):
    data_list=req.data_info["dataset_tid_history_ans"]["data_list"]
    return data_list
def sf_get_data_from_commit(req):

    data_list=req.data_info["dataset_from_commit"]["uid_pid_tid_ans_list"]
    idx=0
    return data_list
def sf_return_data(req,uid_pid_tid_ans_ase_list,pid_truetid_list=None):
    #if uid_pid_tid_ans_ase_list is not None:
        #todo check
    req.data_info["dataset_ans_assessment"]=Util.getDict(
        dataset_usage="dataset_to_saver",
        coll_name="uid_pid_tid_ans_ase_coll",
        field_filter_list=['uid','pid','tid','ans'],
        field_update_list=['ase'],
        field_name_list=['uid','pid','tid','ans','ase'],
        data_list_name="uid_pid_tid_ans_ase_list",
        uid_pid_tid_ans_ase_list=uid_pid_tid_ans_ase_list)
    if pid_truetid_list is not None:
        req.data_info["dataset_pid_judge"]=Util.getDict(
            dataset_usage="dataset_to_saver",
            coll_name="pid_truetid_coll",
            field_filter_list=['pid'],
            field_update_list=['true_tid'],
            field_name_list=['pid','true_tid'],
            data_list_name="pid_truetid_list",
            pid_truetid_list=pid_truetid_list)
    return 1
def test_list_process(test_list,list_name,field_list):
    print("   $$$TEST")
    #print(test_list)
    for sub_set in test_list:
        list=sub_set[list_name]
        for item in list:
            r=0
            for field in field_list:
                r+=item[field]
            if r>3:
                print("valid!")
                item["ase"]=2


class Unit_SampleJudge(object):

    def __init__(self,req,):
        self.req=req
    def excute(self):
        req=self.req
        self.func(req)
    def func(self,req):
        #TEST
        '''
        #sf_get test
        print("$$$$$$TEST")
        user_list= sf_get_data_user_history_ans(req)
        print("0", user_list)
        pic_list = sf_get_data_pic_history_ans(req)
        tag_list = sf_get_data_tag_history_ans(req)
        commit_list=sf_get_data_from_commit(req)
        test_list_process(test_list=user_list,list_name="pid_tid_ans_list",field_list=["ans","tid"])
        print("1",user_list)
        print("$$$$$$TEST")

        '''
        # sf_return test
        print("$$$$$$TEST")
        uid_pid_tid_ans_ase_list=[]
        for uid in range(10):
            for pid in range(20):
                for tid in range(4):
                    doc = Util.getDict(
                        uid=uid + 1, pid=100000001 + pid, tid=tid + 1, ans=random.choice([-1, 0, 1]), ase=random.choice([-1, 1]))
                    uid_pid_tid_ans_ase_list.append(doc)
        pid_truetid_list=[]
        for pid in range(10):
            doc = Util.getDict(pid=100000001 + pid, true_tid=random.choice(range(10)))
            pid_truetid_list.append(doc)
        sf_return_data(req,uid_pid_tid_ans_ase_list=uid_pid_tid_ans_ase_list,pid_truetid_list=pid_truetid_list)
if __name__ == '__main__':
    myReq_list=[]
    for i in range(5):
        input_info={}
        input_info["unit_name"]="SampleJudge"
        input_info["dataset_from_commit"]={}
        input_info["dataset_from_commit"]["uid_pid_tid_ans_list"]=[]
        for j in range(10):
            input_info["dataset_from_commit"]["uid_pid_tid_ans_list"].append(Util.getDict(
                uid=i+1,pid=100000001+j//4,tid=j%4+1,ans=random.choice([-1,0,1]),ase=0))
        print(input_info)
        req=Req(id=i+1,input_info=input_info)
        myReq_list.append(req)

    user_to_fetcher_pipe=Pipe()
    myReqFactory=ReqFactory("myReqFactory",user_to_fetcher_pipe)
    for r in myReq_list:
        myReqFactory.work(r)
    for r in myReq_list:
        print (r.fetch_info)

    myconn = MongoDBBase()
    fetcher_to_unit_pipe = Pipe()
    myFetcher1 = Fetcher("myFetcher1", user_to_fetcher_pipe=user_to_fetcher_pipe,
    fetcher_to_unit_pipe=fetcher_to_unit_pipe, DB_conn=myconn)
    myFetcher1.start()
    myFetcher1.join()

    len=fetcher_to_unit_pipe.get_size()
    idx=0
    while idx<len:
        req=fetcher_to_unit_pipe.get()
        print("------")
        print(req.data_info)
        print("------")
        print("######Now fetcher_to_unit_pipe has %d req left"%fetcher_to_unit_pipe.get_size())
        idx+=1

